import string
from datetime import datetime
from random import randint, sample

from django.core.paginator import Paginator
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from App.models import User


def index(request):
    return render(request, 'index.html')


def add(request):
    users = []
    first = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳"
    last = ["雨莺", "筱萱", "刁丽", "莺拓", "刁洁", ]
    for _ in range(1000):
        username = list(first)[randint(0, len(first) - 1)] + list(last)[randint(0, len(last) - 1)] + ''.join(
            sample(string.ascii_letters + string.digits, 3))
        password = ''.join(sample(string.ascii_letters + string.digits, 10))
        phone = ''.join(sample(string.digits, 10))
        birthday = datetime.now()
        description = ''.join(sample(first, 20))
        users.append(
            User(username=username, password=password, phone=phone, birthday=birthday, description=description))
    User.objects.bulk_create(users)

    return HttpResponse("add successfully")


def login(request):
    return render(request, 'login.html')


def authorize(request):
    if request.method == 'POST':
        password = request.POST.get('password')
        username = request.POST.get('username')
        if User.objects.filter(username=username).exists():
            if User.objects.get(username=username).password == password:
                return render(request, 'frame.html')
            else:
                # 密码错误
                return JsonResponse({'code': 0, 'message': '密码错误'})
                pass
        else:
            # 账号不存在
            return JsonResponse({'code': -1, 'message': '账号不存在'})
            pass
    # 非POST请求则不动
    return render(request, 'login.html')


def message(request):
    # 阿里云短信

    return None


def tgls_adapi(request):
    return render(request, 'tgls/qdAPI.html')


def tgls_iconfont(request):
    return render(request, 'tgls/iconfont.html')


def tgls_agent_agent_add(request):
    return render(request, 'tgls/agent/agent_add.html')


def tgls_agent_agent_list(request):
    return render(request, 'tgls/agent/agent_list.html')


def tgls_goodsManage_goodsType_list(request):
    return render(request, 'tgls/goodsManage/goodsType_list.html')


def tgls_goodsManage_goods_list(request):
    return render(request, 'tgls/goodsManage/goods_list.html')


def tgls_goodsManage_goods_add(request):
    return render(request, 'tgls/goodsManage/goods_add.html')


def tgls_base_base_add(request):
    return render(request, 'tgls/base/base_add.html')


def tgls_base_base_list(request):
    return render(request, 'tgls/base/base_list.html')


def tgls_base_base_customList(request):
    return render(request, 'tgls/base/base_customList.html')


def tgls_base_base_customNewList(request):
    return render(request, 'tgls/base/base_customNewList.html')


def tgls_base_pages(request):
    return render(request, 'tgls/base/pages.html')


def tgls_base_maintain(request):
    return render(request, 'tgls/base/maintain.html')


def tgls_print_outPrintData(request):
    return render(request, 'tgls/print/outPrintData.html')


def tgls_reportForm_reportForm1(request):
    return render(request, 'tgls/reportForm/reportForm1.html')


def tgls_modify_password(request):
    return render(request, 'tgls/modify_password.html')


@csrf_exempt
def pages(request):
    all_users = User.objects.all()
    user_list = []
    for user in all_users:
        user_dict = { 'username': user.username, 'phone': user.phone, 'birthday': user.birthday,
                     'description': user.description}
        user_list.append(user_dict)

    page_index = request.GET.get('pageIndex')
    page_size = request.GET.get('pageSize')
    paginator = Paginator(user_list, page_size)
    contacts = paginator.page(page_index)

    result = {"Code": 101, "Msg": "成功", "DataCount": all_users.count(), "Data": list(contacts)}
    return JsonResponse(result)
